/*
 * http://code.google.com/p/anttesttools/
 * ehcache 使用
 * site: 
 * jar: ehcache-core-*.jar
 */

package anttesttools.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * @author anttesttools
 *
 */
public class CacheUtils
{
    private static Logger log = LoggerFactory.getLogger(CacheUtils.class);

    public static final String CONFIG_CACHE = "configCache";

    /**
     * 增加或更新
     * 
     * @param cacheName
     * @param key
     * @param value
     */
    public static void put(String cacheName, Object key, Object value)
    {
        Cache cache = CacheManager.getInstance().getCache(cacheName);
        cache.put(new Element(key, value));
    }

    /**
     * 获得
     * 
     * @param cacheName
     * @param key
     * @return
     */
    public static Object getObject(String cacheName, Object key)
    {
        Cache cache = CacheManager.getInstance().getCache(cacheName);
        return cache.get(key).getObjectValue();
    }

    /**
     * 删除
     * 
     * @param cacheName
     * @param key
     */
    public static void remove(String cacheName, Object key)
    {
        Cache cache = CacheManager.getInstance().getCache(cacheName);
        cache.remove(key);
    }

    /**
     * 增加新的缓存
     * 
     * @param cacheName
     */
    public static void addCache(String cacheName)
    {
        Cache cache = new Cache(cacheName, 10000, false, true, 0, 0);
        CacheManager.getInstance().addCache(cache);
        log.debug("addCache = {}", cacheName);
    }

    public static void main(String[] args)
    {
        // 创建ehcache的CacheManager
        CacheManager.create();
        CacheUtils.addCache(CacheUtils.CONFIG_CACHE);
        //put--增加、更新
        //get--
        //remove--
    }
}
